package BlockSearch;

import java.util.Scanner;

public class demo2 {
    public static void main(String[] args) {
        int []arr = {27,22,30,40,36,
                     13,19,16,20,
                     7,10,
                     43,50,48};
        Block b1 = new Block(0,4,22,40);
        Block b2 = new Block(5,8,13,20);
        Block b3 = new Block(9,10,7,10);
        Block b4 = new Block(11,13,43,50);
        Block[] blocks = {b1,b2,b3,b4};
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("请输入你要查找的数字");
            int num = sc.nextInt();
            int index = getIndex(arr,blocks,num);
            System.out.println(index);
        }

    }

    private static int getIndex(int[] arr, Block[] blocks, int num) {
        int index = findIndex(blocks,num);
        if (index == -1){
            return -1;
        }


        for(int i = blocks[index].getStartIndex(); i <= blocks[index].getEndIndex(); i++){
            if(arr[i] == num){
                return i;
            }
        }
        return -1;
    }

    public static int findIndex(Block[] blocks,int num){
        for (int i = 0; i < blocks.length; i++) {
            if(num >= blocks[i].getMinNUm() && num <= blocks[i].getMaxNUm()){
                return i;
            }
        }
        return -1;
    }
}
class Block{
    private int startIndex;
    private int endIndex;
    private int minNUm;
    private int maxNUm;

    public Block() {
    }

    public Block(int startIndex, int endIndex, int minNUm, int maxNUm) {
        this.startIndex = startIndex;
        this.endIndex = endIndex;
        this.minNUm = minNUm;
        this.maxNUm = maxNUm;
    }

    /**
     * 获取
     * @return startIndex
     */
    public int getStartIndex() {
        return startIndex;
    }

    /**
     * 设置
     * @param startIndex
     */
    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    /**
     * 获取
     * @return endIndex
     */
    public int getEndIndex() {
        return endIndex;
    }

    /**
     * 设置
     * @param endIndex
     */
    public void setEndIndex(int endIndex) {
        this.endIndex = endIndex;
    }

    /**
     * 获取
     * @return minNUm
     */
    public int getMinNUm() {
        return minNUm;
    }

    /**
     * 设置
     * @param minNUm
     */
    public void setMinNUm(int minNUm) {
        this.minNUm = minNUm;
    }

    /**
     * 获取
     * @return maxNUm
     */
    public int getMaxNUm() {
        return maxNUm;
    }

    /**
     * 设置
     * @param maxNUm
     */
    public void setMaxNUm(int maxNUm) {
        this.maxNUm = maxNUm;
    }

    public String toString() {
        return "Block{startIndex = " + startIndex + ", endIndex = " + endIndex + ", minNUm = " + minNUm + ", maxNUm = " + maxNUm + "}";
    }
}
